package org.lt.resume.util.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.lt.resume.domain.users.User;
import org.lt.resume.util.common.daoInterface.PagenateDAOInterface;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class PagenateDAO extends HibernateDaoSupport implements
		PagenateDAOInterface {

	public List getData(int pageNumber, int pageSize, String sql) {
		//Session session = this.getSessionFactory().getCurrentSession();
//		Session session;
//		if(getSession(true)==null){
//			session= this.getSession(true);
//		} else {
//			session =this.getSessionFactory().getCurrentSession();
//		}
		Session session = getSession(true);
		
		List result = null;
		try {
			Query query = session.createQuery(sql);
			//query = query.setFirstResult(pageSize * (pageNumber - 1));
			query = query.setFirstResult(pageSize * pageNumber);
			query.setMaxResults(pageSize);
			result = query.list();
			session.flush();
			session.cancelQuery();
			session.clear();
			session.disconnect();
			session.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public int getRowsCount(String hql) {
//		Session session;
//		if(getSession(true)==null){
//			session= this.getSession(true);
//		} else {
//			session =this.getSessionFactory().getCurrentSession();
//		}
		Session session = getSession(true);
		Long count = 0l;
		try {
			Query query = session.createQuery(hql);
			count = (Long) query.uniqueResult();
			session.flush();
			session.cancelQuery();
			session.clear();
			session.disconnect();
			session.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return count.intValue();
	}

}
